package com.example.myapplication.found.model;

import com.example.myapplication.found.bean.FoundBean;
import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.bean.StockKlineBean2;
import com.example.myapplication.found.bean.StockNameBean;
import com.example.myapplication.found.file.FoundDayJsonFile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author: rongfeng
 * @date: 2022/8/19
 * @description
 */
public class Fenxi0 extends BaseData {
    public static final int type = 16;
    public volatile static Vector<String> codeList = new Vector<>();
    public volatile static Vector<StockKlineBean2> lastList = new Vector<>();
    public volatile static Vector<String> dateList = new Vector<>();
    public FoundBean foundBean;
    final static long maxShizhi = 100000000L * 200;
    public List<StockNameBean> stockNameBeanList;

    public Fenxi0(StockBean stockBean, StockBean weekStockBean) {
        super(stockBean, weekStockBean);
    }

    public static Vector<Fenxi7.Range> rangeList = new Vector<>();
    public static Vector<Fenxi7.Range> rangeHuanShou = new Vector<>();
    public static Vector<RangResult> rangResults = new Vector<>();

    public static volatile AtomicInteger rangeIndex = new AtomicInteger();
    public static volatile AtomicInteger huanShouIndex = new AtomicInteger();

    static {

//
//        rangeList.add(new Fenxi7.Range(-10, 10));
//        rangeHuanShou.add(new Fenxi7.Range(1, 5f));

        float step = 1f;
        float index1 = -6;
        float index2 = -5f;
        while (index1 <= 5) {
            while (index2 <= 6) {
                rangeList.add(new Fenxi7.Range(index1, index2));
                index2 += step;
            }
            index1 += step;
            index2 = index1 + step;
        }

        index1 = 0;
        index2 = 1f;
        step = 1f;
        while (index1 <= 6) {
            while (index2 <= 6) {
                rangeHuanShou.add(new Fenxi7.Range(index1, index2));
                index2 += step;
            }
            index1 += step;
            index2 = index1 + step;
        }
    }

    @Override
    public void start() {
        if (!codeList.contains(fundCode)) {
            codeList.add(fundCode);
        } else {
            if (callback != null) {
                callback.fenxiCallback(1);
            }
            return;
        }
        Fenxi7.Range range = rangeList.get(rangeIndex.get());
        Fenxi7.Range huanShourange = rangeHuanShou.get(huanShouIndex.get());

        for (int i = 0; i < stockBean.klineList.size(); i++) {
            StockKlineBean bean = stockBean.klineList.get(i);
            if ((bean.chg > range.start && bean.chg <= range.end) &&
                    (bean.zhenfu > huanShourange.start && bean.zhenfu <= huanShourange.end)) {
//                System.out.println(bean.date + " " + fundName + " " + fundCode);
                if (i + 1 < stockBean.klineList.size()) {
//                        for (int j = 1; j < 6; j++) {
                    StockKlineBean lastBean = stockBean.klineList.get(i + 1);
                    StockKlineBean2 stockKlineBean2 = new StockKlineBean2();
                    stockKlineBean2.chg = lastBean.chg;
                    stockKlineBean2.date = lastBean.date;
                    stockKlineBean2.zuoshou = bean.shouPan;
                    stockKlineBean2.code = fundCode;
                    stockKlineBean2.name = fundName;
                    stockKlineBean2.huanshou = bean.huanshou;
                    lastList.add(stockKlineBean2);
//                        }
                }
            }
        }
        callback.fenxiCallback(1);
    }

    public static void analysis() {
        double chgAvg = 0;
        int chgCount = 0;
        int zhang = 0;
        int die = 0;
        double money = 10000;
        for (int i = 0; i < lastList.size(); i++) {
            StockKlineBean2 stockKlineBean = lastList.get(i);
            if (!dateList.contains(stockKlineBean.date)) {
                dateList.add(stockKlineBean.date);
            }
        }

        List<String> collect = dateList.stream().sorted(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        }).collect(Collectors.toList());

        for (String date : collect) {
            List<StockKlineBean2> dealList = new ArrayList<>();
            for (StockKlineBean2 stockKlineBean : lastList) {
                if (date.equals(stockKlineBean.date)) {
                    dealList.add(stockKlineBean);
                    if (dealList.size() >= 4) {
                        break;
                    }
                }
            }

            double itemMoney = money / 1;
            double shouyi = 0;
            double dayShouyi = 0;
            for (StockKlineBean2 stockKlineBean : dealList) {
                int shou = (int) (itemMoney / (stockKlineBean.zuoshou * 100));
                if (shou <= 0) {
                    continue;
                }
                double byMoney = shou * (stockKlineBean.zuoshou * 100);

                double shouxu = byMoney * 0.00022f;

                money = money - byMoney - shouxu;

                shouyi += byMoney + byMoney * (stockKlineBean.chg / 100f);

                double shouxu3 = shouyi * 0.00022f;

                shouyi = shouyi - shouxu3;
//
//                System.out.println(stockKlineBean.code + " " + stockKlineBean.name + " " + stockKlineBean.chg + " : " + stockKlineBean.huanshou);
                chgAvg += stockKlineBean.chg;
                chgCount++;
                if (stockKlineBean.chg > 0) {
                    zhang++;
                } else if (stockKlineBean.chg < 0) {
                    die++;
                }
            }
            money += shouyi;
//            System.out.println(date);
//            System.out.println("余额 " + money);
//            System.out.println();
        }

        Fenxi7.Range range = rangeList.get(rangeIndex.get());
        Fenxi7.Range huanShourange = rangeHuanShou.get(huanShouIndex.get());
        RangResult rangResult = new RangResult();
        rangResult.chgRange = range;
        rangResult.huanShourange = huanShourange;
        if (zhang > 0 && die > 0) {
            rangResult.zhang = zhang;
            rangResult.die = die;
            rangResult.chgAvg = (chgAvg / chgCount);
            rangResult.money = money;
            rangResults.add(rangResult);
        }


//        System.out.println("S ST *ST 股票");
//        System.out.println("涨幅范围  " + range.start + " " + range.end);
//        System.out.println("振幅范围  " + huanShourange.start + " " + huanShourange.end);
//        System.out.println("涨次数 " + zhang + " " + ((float) zhang / (zhang + die)) * 100);
//        System.out.println("跌次数 " + die + " " + ((float) die / (zhang + die)) * 100);
//        System.out.println("涨跌幅平均 " + (chgAvg / chgCount));
//        System.out.println("money " + money);

        System.out.print(huanShourange.start);

        lastList.clear();
        codeList.clear();
        dateList.clear();
    }

    public static class RangResult implements Serializable {
        public Fenxi7.Range chgRange;
        public Fenxi7.Range huanShourange;
        public float zhang;
        public float die;
        public double chgAvg;
        public double money;
    }

    public static void analysisEnd() {

        List<RangResult> collect = rangResults.stream().sorted(new Comparator<RangResult>() {
            @Override
            public int compare(RangResult o1, RangResult o2) {
                if (o1.money == o2.money) {
                    return 0;
                } else if (o1.money - o2.money > 0) {
                    return 1;
                } else {
                    return -1;
                }
            }
        }).collect(Collectors.toList());

//        collect = collect.stream().sorted(new Comparator<RangResult>() {
//            @Override
//            public int compare(RangResult o1, RangResult o2) {
//                if (o1.chgAvg == o2.chgAvg) {
//                    return 0;
//                } else if (o1.chgAvg - o2.chgAvg > 0) {
//                    return 1;
//                } else {
//                    return -1;
//                }
//            }
//        }).collect(Collectors.toList());


//        FoundDayJsonFile foundDayJsonFile = new FoundDayJsonFile();
//        foundDayJsonFile.saveRang(collect);

        for (RangResult result : collect) {
            System.out.println("涨幅范围  " + result.chgRange.start + " " + result.chgRange.end);
            System.out.println("振幅范围  " + result.huanShourange.start + " " + result.huanShourange.end);
            System.out.println("涨次数 " + result.zhang);
            System.out.println("跌次数 " + result.die);
            System.out.println("胜率 " + (float) result.zhang / (result.zhang + result.die) * 100 + "%");
            System.out.println("赔率 " + result.chgAvg);
            System.out.println("money " + result.money);
            System.out.println();
        }

    }

}
